package de.gwtprime.client.views;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.Widget;

import de.gwtprime.client.PrimeService;
import de.gwtprime.client.PrimeServiceAsync;
import de.gwtprime.client.places.ImpressumPlace;
import de.gwtprime.client.util.AppUtils;
import de.gwtprime.client.util.FacebookWidget;
import de.gwtprime.client.util.GWTUtils;
import de.gwtprime.client.util.GooglePlusWidget;

public class FooterViewImpl extends Composite implements FooterView{

	FooterViewImplUiBinder uiBinder = GWT.create(FooterViewImplUiBinder.class);
	interface FooterViewImplUiBinder extends UiBinder<Widget, FooterViewImpl>{}
	
	Presenter presenter;
	
	@UiField TextBox nameInput;
	@UiField TextBox emailInput;
	@UiField TextArea messageArea;
	@UiField Button sendMailBtn; 
	@UiField Button seeMoreBtn;
	@UiField Anchor impressumAnchor;
	@UiField SimplePanel fbPanel;
	@UiField SimplePanel googlePlulPanel;
	
	public FooterViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
		nameInput.setValue(GWTUtils.I18n.name());
		emailInput.setValue(GWTUtils.I18n.email());
		messageArea.setValue(GWTUtils.I18n.comment());
		sendMailBtn.setText(GWTUtils.I18n.sendMessageBtn());
		impressumAnchor.setText(GWTUtils.I18n.impressumLink());
		seeMoreBtn.setText(GWTUtils.I18n.seeMoreBtn());
		
		fbPanel.add(new FacebookWidget());
		googlePlulPanel.add(new GooglePlusWidget());
		ClickHandler clickHandler = new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				TextBoxBase source = (TextBoxBase)event.getSource();
				
				
				// If user typed something different than the default -> don't change it and exit this method.
				if(nameInput.equals(source) && !nameInput.getValue().equals(GWTUtils.I18n.name())){
					return;
				}else if(emailInput.equals(source) && !emailInput.getValue().equals(GWTUtils.I18n.email())){
					return;
				}else if(messageArea.equals(source) && !messageArea.getValue().equals(GWTUtils.I18n.comment())){
					return;
				}
				
				source.setValue("");
				
			}
		};
		
		nameInput.addClickHandler(clickHandler);
		emailInput.addClickHandler(clickHandler);
		messageArea.addClickHandler(clickHandler);
		
		
		BlurHandler blurHandler = new BlurHandler() {
			
			@Override
			public void onBlur(BlurEvent event) {
				TextBoxBase source = (TextBoxBase)event.getSource();
				
				// if user alread typed something -> don't set back to default.
				if(!source.getValue().equals("")) return;
				
				if(nameInput.equals(source)){
					nameInput.setValue(GWTUtils.I18n.name());
				}else if(emailInput.equals(source)){
					emailInput.setValue(GWTUtils.I18n.email());
				}else if(messageArea.equals(source)){
					messageArea.setValue(GWTUtils.I18n.comment());
				}
				
			};
		};
		nameInput.addBlurHandler(blurHandler);
		emailInput.addBlurHandler(blurHandler);
		messageArea.addBlurHandler(blurHandler);
		
		//AppUtils.initializeGooglePlusWidget("");
	}
	
	@UiHandler("sendMailBtn")
	public void sendMessage(ClickEvent e){
		/**
		 * Create a remote service proxy to talk to the server-side Prime service.
		 */
		final PrimeServiceAsync primeService = GWT
				.create(PrimeService.class);
		
		primeService.sendMail(nameInput.getText(), emailInput.getText(), messageArea.getText(), new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {

				PopupPanel popup = new PopupPanel(true);
				popup.add(new Label(GWTUtils.I18n.msgSuccessMsg()));
				popup.setTitle("mypopup");
				popup.center();
				
				nameInput.setValue(GWTUtils.I18n.name());
				emailInput.setValue(GWTUtils.I18n.email());
				messageArea.setValue(GWTUtils.I18n.comment());
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				
				PopupPanel popup = new PopupPanel(true);
				popup.add(new Label(GWTUtils.I18n.msgFailedMsg()));
				popup.setTitle("mypopup");
				popup.center();
				
				nameInput.setValue(GWTUtils.I18n.name());
				emailInput.setValue(GWTUtils.I18n.email());
				messageArea.setValue(GWTUtils.I18n.comment());
				
			}
		});
	}
	
	@UiHandler("impressumAnchor")
	public void impressumClick(ClickEvent e){
		presenter.goTo(new ImpressumPlace("Impressum"));
		Window.scrollTo(0, 0);	
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		
	}
}
